<template>
  <div style="margin-left: 19px">
    <span style="font-size: 15px; margin-bottom: 20px; display: block">{{
      `附件上传（${contents}）`
    }}</span>
    <el-upload
      :disabled="disableds"
      :action="interfaceUrl"
      list-type="picture-card"
      :limit="5"
      multiple
      :auto-upload="false"
      :file-list="fileList"
      :on-remove="handleRemove"
      :on-preview="handlePictureCardPreview"
      :on-change="handleEditChange"
      :on-exceed="exceed"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fileList: [],
      interfaceUrl: "http://101.42.233.226:9000/", // 上传服务器的图片地址 图床的
      dialogImageUrl: "",
      dialogVisible: false,
      contents: this.content,
      disableds: false,
    };
  },
  props: ["content", "clearPics"],
  inheritAttrs: false,
  emits: ["uploadImages"],
  watch: {
    clearPics(n) {
      this.fileList = [];
    },
  },
  methods: {
    // 移除图片触发
    handleRemove(file, fileList) {
      console.log(file);
      this.fileList = fileList;
      console.log(this.fileList);
      this.$emit("uploadImages", this.fileList, this.contents);
    },
    // 上传图片触发
    handleEditChange(file, fileList) {
      console.log(file);
      const isLt2M = file.size < 524131;
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过500k!");
        fileList.pop();
      }
      this.fileList = fileList;
      this.$emit("uploadImages", this.fileList, this.contents);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 上传数量超过5个回调
    exceed() {
      this.$message({
        message: "最多可以上传5张图片",
        type: "warning",
      });
    },
  },
};
</script>

<style  scoped>
.avatar {
  width: 150px;
  height: 145px;
  border-radius: 5%;
  float: left;
}
</style>
